package org.seven.cpcrkapp.util;

import android.content.Context;
import android.content.SharedPreferences;

import org.seven.cpcrkapp.App;

public abstract class WebAppUtils {
    private static String FILE_NAME = "WEB_APP";
    private static String WEB_APP_IP = "WEB_APP_IP";
    private static String WEB_APP_PORT = "WEB_APP_PORT";
    private static String WEB_APP_NAME = "WEB_APP_NAME";

    public static String getURL() {
        return String.format("http://%s:%s%s", getIp(), getPort(), getName());
    }

    public static String getIp() {
        SharedPreferences sharedPreferences = App.getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(WEB_APP_IP, "192.168.1.103");
    }

    public static String getPort() {
        SharedPreferences sharedPreferences = App.getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(WEB_APP_PORT, "8080");
    }

    public static String getName() {
        SharedPreferences sharedPreferences = App.getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(WEB_APP_NAME, "/cpcrkxt");
    }

    public static void save(String ip, String port, String name) {
        SharedPreferences.Editor editor = App.getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(WEB_APP_IP, ip);
        editor.putString(WEB_APP_PORT, port);
        editor.putString(WEB_APP_NAME, name);
        editor.apply();
    }

    public static void clear(){
        SharedPreferences.Editor editor = App.getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
    }
}